import { getFaultRecord } from "@/api/fault-record"
import { useFaultRecord } from "@/stores"
import { inject, provide, reactive, ref } from "vue"


export class MainContext {
  list = ref([] as FaultRecordDto[])
  searchValue = ref('')
  faultRecordStore = useFaultRecord()
  state = reactive({
    isAdmin: false
  })

  async search() {
    if (!this.searchValue.value) return
    const list = await getFaultRecord({ key: this.searchValue.value })
    if (this.searchValue.value === '11111111') this.state.isAdmin = true
    this.list.value = list
  }

  input = uni.$util.throttle(() => this.search(), 1000)


  cat(record: FaultRecordDto) {
    let url = this.state.isAdmin ? '/pages/add/index' : '/pages/detail/index'
    this.faultRecordStore.set(record)
    uni.navigateTo({ url })
  }

  add() {
    this.faultRecordStore.clear()
    uni.navigateTo({ url: '/pages/add/index' })
  }


  provide() {
    provide('MainContext', this)
  }
  static inject() {
    return inject('MainContext') as MainContext
  }

}